package com.prb.tomcat.utils;

import com.prb.tomcat.handler.ServletHandler;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtil {



    public static ByteBuffer readFile(String path) throws IOException {
        path = ServletHandler.class.getClassLoader().getResource("templates").getPath()  + (path.endsWith("/") ? path + "index.html" : path);
        RandomAccessFile raf = new RandomAccessFile(path, "r");
        FileChannel channel = raf.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);

        buffer.flip();
        return buffer;
    }
}
